﻿@using IOA.MES.DataAccess.VModel
@{
    ViewBag.Title = "标片绑定";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var workOrderPieces = ViewBag.WordOrderStandPieces as List<VWorkOrderStandardPieceInfo>;
    var pieces = ViewBag.StandardPieces as List<VStandardPiece>;
}
@model IOA.MES.DataAccess.VModel.VStandardPiece

@section Contents{
    <style>
        html {
            background-color: #FFF;
        }

        .layui-form-item .layui-input-inline {
            width: 300px;
        }

        .red {
            color: red;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div>
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:130px;">{{lang.Field.VerificationFrequency}}：</label>
                        <div class="layui-input-inline" style="width:100px;">
                            <input type="number" id="txtFrequency" name="Frequency" value="@(!workOrderPieces.Any()||!workOrderPieces.FirstOrDefault().Frequency.HasValue?string.Empty:workOrderPieces.FirstOrDefault().Frequency.Value.ToString())" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.StandardTabletCode}}：</label>
                        <div class="layui-input-inline">
                            <select id="selPieces" lay-filter="selPieces" lay-search="">
                                <option value="0" selected>选择标片</option>
                                @foreach (var item in pieces)
                                {
                                    <option value="@item.PkId">@item.ProductCode</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="submit"><i class="layui-icon">&#xe605;</i>{{lang.Field.Submit}}</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        var orderNo = "@ViewBag.OrderNo";
        var workOrderNo = "@ViewBag.WorkOrderNo";
        var pieces = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(pieces));
        var workOrderPieces = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(workOrderPieces));

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['form', 'layer', 'index', 'laydate', 'table'], function () {
                var layer = layui.layer,
                    form = layui.form,
                    table = layui.table;

                var options = {
                    elem: '#table'
                    , id: "table"
                    , limit: 1000
                    , cols: [[ //标题栏
                        { title: '序号', width: 50, type: "numbers" }
                        , { field: 'ProductCode', title: '标片条码', minWidth: 200 }
                        , { field: 'Model', title: '型号', minWidth: 130 }
                        , {
                            title: 'Pmax', minWidth: 130, templet: function (piece) {
                                return piece.Pmax + (piece.PmaxToleranceUp == piece.PmaxToleranceDown ? ("±" + piece.PmaxToleranceDown) : ("-" + piece.PmaxToleranceDown + "+" + piece.PmaxToleranceUp));
                            }
                        }
                        , {
                            title: 'Voc', minWidth: 130, templet: function (piece) {
                                return piece.Voc + (piece.VocToleranceUp == piece.VocToleranceDown ? ("±" + piece.VocToleranceDown) : ("-" + piece.VocToleranceDown + "+" + piece.VocToleranceUp));
                            }
                        }
                        , {
                            title: 'Isc', minWidth: 130, templet: function (piece) {
                                return piece.Isc + (piece.IscToleranceUp == piece.IscToleranceDown ? ("±" + piece.IscToleranceDown) : ("-" + piece.IscToleranceDown + "+" + piece.IscToleranceUp));
                            }
                        }
                        , {
                            title: 'Vmp', minWidth: 130, templet: function (piece) {
                                return piece.Vmp + (piece.VmpToleranceUp == piece.VmpToleranceDown ? ("±" + piece.VmpToleranceDown) : ("-" + piece.VmpToleranceDown + "+" + piece.VmpToleranceUp));
                            }
                        }
                        , {
                            title: 'Imp', minWidth: 130, templet: function (piece) {
                                return piece.Imp + (piece.ImpToleranceUp == piece.ImpToleranceDown ? ("±" + piece.ImpToleranceDown) : ("-" + piece.ImpToleranceDown + "+" + piece.ImpToleranceUp));
                            }
                        }
                        , {
                            title: 'FF', minWidth: 130, templet: function (piece) {
                                return piece.FF + (piece.FFToleranceUp == piece.FFToleranceDown ? ("±" + piece.FFToleranceDown) : ("-" + piece.FFToleranceDown + "+" + piece.FFToleranceUp));
                            }
                        }
                        , {
                            title: '操作', width: 100, fixed: 'right', templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-danger delPiece" code="' + d.ProductCode + '"><i class="layui-icon">&#xe640;</i>删除</button>';
                                return str;
                            }
                        }
                    ]]
                    , data: workOrderPieces
                    , limit: 1000
                    , even: true
                };
                table.render(options);

                form.on('select(selPieces)', function (data) {
                    if (pieces.length <= 0) {
                        return;
                    }

                    var piece = pieces.find(function (e) {
                        return e.PkId == $("#selPieces").val();
                    });
                    if (piece && workOrderPieces.find((e) => { return e.ProductCode == piece.ProductCode; }) == null) {
                        workOrderPieces.push(piece);
                        options.data = workOrderPieces;
                        table.reload("table", options);
                    }
                });

                $(document).on("click", ".delPiece", function () {
                    var barCode = $(this).attr("code").trim();
                    for (var i = 0; i < workOrderPieces.length; i++) {
                        if (workOrderPieces[i].ProductCode == barCode) {
                            workOrderPieces.splice(i, 1);
                            break;
                        }
                    }
                    options.data = workOrderPieces;
                    table.reload("table", options);
                });

                form.on('submit(submit)', function (data) {
                    var productCodes = workOrderPieces.map((e) => e.ProductCode);
                    console.log(productCodes);
                    var loadId = layer.load();
                    $.ajax({
                        url: "/ParaSetting/SaveStandardPiece",
                        data: {
                            orderNo: orderNo,
                            workOrderNo: workOrderNo,
                            productCodes: productCodes,
                            frequency: $("#txtFrequency").val().trim() == "" ? null : parseFloat($("#txtFrequency").val().trim())
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            layer.msg("保存成功！", { icon: 6, time: 1500 });
                        }
                    });
                    return false;
                });
            });
        }
    </script>
}
